package br.com.wssa.smmb.security;

import java.util.Collection;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import br.com.wssa.smmb.entidades.Role;
import br.com.wssa.smmb.entidades.Usuario;

public class SecurityAuthentication implements Authentication {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final Usuario usuario;
	private boolean autenticado;

	public SecurityAuthentication(Usuario usuario, Collection<Role> permissoes) {
		this.usuario = usuario;
		this.permissoes = permissoes;
	}

	public String getName() {
		return usuario != null ? usuario.getNome() : null;
	}

	private Collection<Role> permissoes;

	public Collection<? extends GrantedAuthority> getAuthorities() {

		return permissoes;
	}

	public Object getCredentials() {
		return usuario != null ? usuario.getHashSenha() : null;
	}

	public Object getDetails() {
		return usuario;
	}

	public Object getPrincipal() {
		if (usuario != null) {
			if (StringUtils.isNotBlank(usuario.getNome())) {
				String[] primeiroNome = usuario.getNome().split(" ");
				return primeiroNome[0];
			}
		}
		return null;
	}

	public boolean isAuthenticated() {
		return this.autenticado;
	}

	public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
		this.autenticado = isAuthenticated;

	}

}
